package edu.itstudy.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

/**
 * 菜单选择
 * 最大输入size
 */
public class ScannerUtil {

    private static Scanner sc = new Scanner(System.in);

    /**
     * 读取指定长度序号
     * @return
     */
    public static int readInt(){
        int i =0 ;
        while(true) {
            String str = readkeyBoard(3, false);
            try {
               i = Integer.parseInt(str);
            } catch (NumberFormatException e) {
                System.out.println("请选择序号输入：");
                continue;
            }
            break;
        }
        return  i;
    }

    /**
     * 日期格式
     * @param format
     * @return
     */

    public static Date readDate(String format){
     Date date = null;
      while(true){
          String date_str = readkeyBoard(format.length(),false);
          SimpleDateFormat sdf = new SimpleDateFormat(format);
          try {
              date = sdf.parse(date_str);
          } catch (ParseException e) {
              System.out.println("输入有误，重新输入");
              continue;
          }
          break;
      }
      return  date;
    }
    //重载，
    public static Date readDate(String format,Date defaultValue){
    Date date = null;

    while(true){
        String date_str = readkeyBoard(format.length(),true);
        if("".equals(date_str)){
            return defaultValue;
        }

        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            date = sdf.parse(date_str);
        } catch (ParseException e) {
            System.out.println("输入有误，重新输入");
            continue;
        }
        break;
    }
    return  date;
}


    /**
     * 输入字符串
     * @param limit 最大长度
     * @return
     */
    public static String  readString(int limit){
        return readkeyBoard(limit,false);

    }
    //重载,输入字符串,为空返回默认值
    public static String  readString(int limit,String defaultValue){
        String str = readkeyBoard(limit,true);
        return "".equals(str) ? defaultValue : str;
    }
    /**
     * 判断输入范围是否对
     * @param size 判断输入范围是否对
     * @return
     */
    public static char readMenuSelect(int size){
        char c = ' ';
        while(true){
            String str = readkeyBoard(1,false);
            c = str.charAt(0);

            boolean r = true;
            for(int i= 1; i <= size;i++ ) {
               r = r && (c != i+'0');
                if(!r){
                    break;
                }
            }

            if(r){
                System.out.println("选择有误，重新输入");
            }else{
                break;
            }
        }
        return c;
    }

    /**
     * 输入工具
     * limit 输入长度限制
     * blankReturn  是否能输入空 boolean
     */
    public static String readkeyBoard(int limit,boolean blankReturn){

        String line = "";
        while(sc.hasNextLine()){
            line = sc.nextLine();
            line = line.trim();//去掉前后空格
            if (line.length() == 0) {
                if (blankReturn) {
                    return line;
                } else {
                    continue;
                }
            }
            if(line.length()>limit){
                System.out.println("输入长度有误(不大于"+limit+"),重新输入：");
                continue;
            }
            break;
        }
        return line;
    }

}